﻿using System;
using System.Diagnostics;
using System.Text.Json;

namespace _JsonRealizer
{

    public class JsonRealizer
    {
        private static int count = 0;
        private static double x_up;
        private static double y_up;
        private static double x_down;
        private static double y_down;
        private static bool _iscomplate;
        readonly static double averagefactor = 3;


        public static bool GetRootObject(string json)
        {
            RootObject result = JsonSerializer.Deserialize<RootObject>(json);

            foreach (BodyJointsResult bodyJointResult in result.BodyJointsResults)
            {
                foreach (BodyJoint bodyJoint in bodyJointResult.BodyJoints)
                {
                    if (bodyJoint.BodyScore < 0.25)
                    {
                        _iscomplate = false;
                        break;
                    }
                    else { _iscomplate = true; }

                }
            }
            if (_iscomplate)
            {
                foreach (BodyJointsResult bodyJointResult in result.BodyJointsResults)
                {
                    foreach (BodyJoint bodyJoint in bodyJointResult.BodyJoints)
                    {
                        //Console.WriteLine(bodyJoint.KeyPointType);
                        //Console.WriteLine("x:"+bodyJoint.X);
                        //Console.WriteLine("y"+bodyJoint.Y);
                        //Console.WriteLine("置信度"+bodyJoint.BodyScore);
                        if (bodyJoint.KeyPointType == "头部" || bodyJoint.KeyPointType == "颈部" || bodyJoint.KeyPointType == "左肩" || bodyJoint.KeyPointType == "右肩")
                        {
                            if (bodyJoint.BodyScore < 0.3)
                            {
                                bodyJoint.X = 0;
                                bodyJoint.Y = 0;
                            }
                            x_up += bodyJoint.X;
                            y_up += bodyJoint.Y;


                        }
                        if (bodyJoint.KeyPointType == "左膝" || bodyJoint.KeyPointType == "右膝" || bodyJoint.KeyPointType == "左踝" || bodyJoint.KeyPointType == "右踝")
                        {
                            if (bodyJoint.BodyScore < 0.3)
                            {
                                bodyJoint.X = 0;
                                bodyJoint.Y = 0;
                            }
                            x_down += bodyJoint.X;
                            y_down += bodyJoint.Y;
                        }

                    }
                    x_up = x_up / averagefactor;
                    y_up = y_up / averagefactor;
                    x_down = x_down / averagefactor;
                    y_down = y_down / averagefactor;
                    double Height = Math.Abs(y_up - y_down);
                    Debug.WriteLine("高度差："+Height.ToString());
                    double Wdth = Math.Abs(x_up - x_down);
                    Debug.WriteLine("水平差：" + Wdth.ToString());
                    if (Wdth > Height / 4)
                    {
                        return false;
                    }
                }
            }

            return true;

        }

    }
}

